﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Bussines.Utils;
using Entities;

namespace Website.Administrator
{
    public partial class DanhMucDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                String rqMa = Request.QueryString["MaDanhMucEdit"];
                String rqMaCap2 = Request.QueryString["Macap2"];
                DanhMucService danhMucService = new DanhMucService();
                LoadDataDDL(2, ddlDmCha);
                int maCap1 = Int32.Parse(ddlDmCha.SelectedValue.ToString());
                LoadDataDDL(maCap1, ddlCap2);

                if (rqMa != null)
                {
                    ddlDmCha.Items.Clear();
                    DanhMuc danhMuc = new DanhMuc();
                    danhMuc = danhMucService.Select(Int32.Parse(rqMa));
                    int piddm = danhMuc.Pid;
                    String ten = danhMuc.Ten;
                    int madmcha = danhMuc.Id;
                    danhMuc = danhMucService.Select(piddm);
                    ddlDmCha.Items.Add(new ListItem(danhMuc.Ten, danhMuc.Id.ToString()));
                    txtTenDanhMuc.Text = ten;
                    hdfMaDanhMuc.Value = madmcha.ToString();
                    ibtThemMoi.Visible = false;
                    ibtUpdate.Visible = true;
                    VisibleDDL(false);
                    CheckBox1.Visible = false;
                }
                else if (rqMaCap2 != null)
                {
                    ddlCap2.Items.Clear();
                    String rqMaCap1 = Request.QueryString["Macap1"];
                    DanhMuc danhMuc = new DanhMuc();
                    danhMuc = danhMucService.Select(Int32.Parse(rqMaCap1));
                    ddlCap2.Items.Add(new ListItem(danhMuc.Ten, danhMuc.Id.ToString()));
                    DanhMuc danhMucCha = new DanhMuc();
                    danhMucCha = danhMucService.Select(danhMuc.Pid);
                    ddlDmCha.Items.Clear();
                    ddlDmCha.Items.Add(danhMucCha.Ten);
                    //LoadDataDDL(danhMuc.Pid,ddlCap2);

                    danhMuc = danhMucService.Select(Int32.Parse(rqMaCap2));
                    hdfMaDanhMuc.Value = danhMuc.Id.ToString();
                    txtTenDanhMuc.Text = danhMuc.Ten;
                    CheckBox1.Visible = false;
                    ibtThemMoi.Visible = false;
                    ibtUpdate.Visible = true;
                }
                else
                {
                    ibtThemMoi.Visible = true;
                    ibtUpdate.Visible = false;

                }

            }
        }

        public void LoadDataDDL(int pid, DropDownList nameDDL)
        {
            DanhMucService danhMucService = new DanhMucService();
            List<DanhMuc> danhMucs = new List<DanhMuc>();
            Hashtable hashtable = new Hashtable();
            hashtable["pid"] = pid;
            danhMucs = danhMucService.Search(hashtable, "id", "DESC", 0);
            if (danhMucs.Count < 1)
            {
                VisibleDDL(false);
                return;
            }
            else if (!CheckBox1.Checked)
            {
                VisibleDDL(true);
            }
            nameDDL.DataSource = danhMucs;
            nameDDL.DataTextField = "ten";
            nameDDL.DataValueField = "id";
            nameDDL.DataBind();
        }
        private void VisibleDDL(bool giatri)
        {
            ddlCap2.Visible = giatri;
            lblDanhMucCap2.Visible = giatri;
            if (giatri)
            {
                MultiView1.ActiveViewIndex = 0;
                MultiView2.ActiveViewIndex = 0;
            }
            else
            {
                MultiView1.ActiveViewIndex = -1;
                MultiView2.ActiveViewIndex = -1;
            }


        }
        protected void ibtThemMoi_Click(object sender, ImageClickEventArgs e)
        {
            DanhMucService dmSevice = new DanhMucService();
            DanhMuc dMuc = new DanhMuc();
            dMuc.Ten = txtTenDanhMuc.Text;
            int pid = 0;
            if (ddlCap2.Visible)
            { pid = Int32.Parse(ddlCap2.SelectedValue.ToString()); }
            else
            {
                pid = Int32.Parse(ddlDmCha.SelectedValue.ToString());
            }
            dMuc.Pid = pid;
           
            dmSevice.Insert(dMuc);
            MessageBox.Show("insert thành công!");
        }

        protected void ibtUpdate_Click(object sender, ImageClickEventArgs e)
        {
            DanhMucService dmSevice = new DanhMucService();
            DanhMuc dMuc = dmSevice.Select(Int32.Parse(hdfMaDanhMuc.Value.ToString()));
            if (Request.QueryString["MaDanhMucEdit"] != null)
            {

                dMuc.Ten = txtTenDanhMuc.Text;
                int pid = Int32.Parse(ddlDmCha.SelectedValue.ToString());
                if (pid != 0)
                    dMuc.Pid = pid;
               
                dmSevice.Update(dMuc);
                MessageBox.Show("Update thành công!");
                Response.Redirect("DanhMucList.aspx?MaDanhMucCha=" + dMuc.Pid);
            }
            else
            {
                dMuc.Ten = txtTenDanhMuc.Text;
                int pid = Int32.Parse(ddlCap2.SelectedValue.ToString());
                if (pid != 0)
                    dMuc.Pid = pid;
             
                dmSevice.Update(dMuc);
                MessageBox.Show("Update thành công!");
                Response.Redirect("DanhMucList.aspx?MaDanhMucCha=" + Request.QueryString["Macap1"] + "&danhmuccap2=" + dMuc.Pid);
            }

        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (Request.QueryString["MaDanhMucEdit"] != null)
            {
                Response.Redirect("DanhMucDetail.aspx?MaDanhMucEdit=" + Request.QueryString["MaDanhMucEdit"]);
            }
            else
            {
                Response.Redirect("DanhMucDetail.aspx.aspx");
            }
        }

        protected void ddlDmCha_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maCap1 = Int32.Parse(ddlDmCha.SelectedValue.ToString());
            LoadDataDDL(maCap1, ddlCap2);
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked)
            {
                VisibleDDL(false);
            }
            else
            {
                VisibleDDL(true);
                int maCap1 = Int32.Parse(ddlDmCha.SelectedValue.ToString());
                LoadDataDDL(maCap1, ddlCap2);
            }
        }
    }
}